// This code is released under the BSD license.
namespace PugLib
{
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// Static type used to format a list of objects into a string. Each object has ToString called and
    /// is concatenated by a StringBuilder and seperated by a pipe (|).
    /// 
    /// http://www.codeplex.com/UkadcDiagnostics
    /// </summary>
    public static class StringFormatter
    {
        /// <summary>
        /// Formats a list of objects into a string. Each object has ToString called and
        /// is concatenated by a StringBuilder and seperated by a pipe (|).
        /// </summary>
        /// <param name="separator">defaults to "|" if null or empty</param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string FormatData<T>(string separator, IEnumerable<T> data)
        {
            if (string.IsNullOrEmpty(separator))
            {
                separator = "|";
            }

            string ret = null;
            if (null != data)
            {
                StringBuilder sb = new StringBuilder();

                foreach (T obj in data)
                {
                    if (null == obj)
                    {
                        continue;
                    }

                    if (sb.Length > 0)
                    {
                        sb.Append(separator);
                    }
                    sb.Append(obj);
                }
                ret = sb.ToString();
            }
            return ret;
        }
    }
}